﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using System.Data;
using OF.Common;
using System.Configuration;
namespace OF.BLL.User
{
    /// <summary>
    /// 买家注册、登陆接口
    /// </summary>
    public class User
    {

        DAL.Buyer dal_buyer = new DAL.Buyer();
        DAL.Seller dal_seller = new DAL.Seller();
        DAL.FeedBack dal_feedback = new DAL.FeedBack();
        #region 买家注册 user.reg
        public string doSendUser(string loginname, string pwd,string address)
        {
            string result = Enmus.RETURN.FAIL.ToString();
            try
            {
                var ds = dal_buyer.GetList("loginname='" + loginname + "' and state != 2");
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    result = Enmus.RETURN.EXISTS.ToString();
                }
                else
                {
                    string phone = loginname;
                    int id = dal_buyer.Add(loginname, pwd, phone);
                    if (id > 0)//Utils.Md5(pwd)
                    {
                        result = id.ToString();
                        //添加默认地址
                        if (!string.IsNullOrWhiteSpace(address)) {
                            DAL.BuyerAddress dal_address = new DAL.BuyerAddress();
                            dal_address.Add(address, result,"2");
                        }
                    }
                }

            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doSendUser");
            }
            return result;
        }
        #endregion

        #region 买家登录 user.login
        public string doValidLogin(string loginname, string pwd)
        {
            string result = Enmus.RETURN.FAIL.ToString();
            try
            {
                DataSet ds = dal_buyer.GetNameAndAddress(loginname , pwd );//Utils.Md5(pwd)
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("BuyerInfo", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doValidLogin");
            }
            return result;
        }
        #endregion

        #region 买家留言反馈 user.feedback.add
        public string doSendFeedBackInfo(string usertype, string contact, string message)
        {
            string result = Enmus.RETURN.FAIL.ToString();
            try
            {
                if (string.IsNullOrEmpty(usertype) || string.IsNullOrEmpty(message))
                {
                    return result;
                }
                Model.FeedBack model = new Model.FeedBack();
                model.addtime = DateTime.Now;
                if (usertype == "seller")
                {
                    model.comefrom = 2;
                }
                else
                {
                    model.comefrom = 1;
                }
                model.contact = contact;
                model.message = message;
                model.isdeal = 0;
                if (dal_feedback.Add(model)>0)
                {
                    result = Enmus.RETURN.SUC.ToString();
                }

            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doSendFeedBackInfo");
            }
            return result;
        }
        #endregion


        #region 卖家登录 user.login
        public string LoginSeller(string loginname, string pwd)
        {
            string result = Enmus.RETURN.FAIL.ToString();
            try
            {
                DataSet ds = dal_seller.GetList("loginname='" + loginname + "' and pwd ='" + pwd + "' and state != 2");//Utils.Md5(pwd)
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    result = ds.Tables[0].Rows[0]["id"].ToString();
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "LoginSeller");
            }
            return result;
        }
        #endregion


        #region 获取商家列表 user.sellerlist.get
        public string doGetAllSellerList()
        {
            string result = Enmus.RETURN.NULL.ToString();
            try
            {
                DataSet ds = dal_seller.GetList(" state = 1");
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("AllSellerList", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doGetAllSellerList");
            }
            return result;
        }
        #endregion

        #region 根据商家类型筛选商家 user.sellerbytype.get
        public string doGetSellerListByType(string shoptype)
        {
            string result = Enmus.RETURN.NULL.ToString();
            try
            {
                DataSet ds = dal_seller.GetList("  shoptype =" + shoptype + " and state = 1 ");
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("GetSellerListByType", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doGetSellerListByType");
            }
            return result;
        }
        #endregion

        #region web端 买家登录
        public int doWebValidLogin(string loginname, string pwd)
        {
            int id = 0;
            try
            {
                DataSet ds = dal_buyer.GetList("loginname='" + loginname + "' and pwd ='" + pwd + "'");//Utils.Md5(pwd)
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    id = Convert.ToInt32(ds.Tables[0].Rows[0]["id"].ToString());
                }
            }
            catch (Exception e)
            {

                Common.LogHelper.WriteLog(e.ToString(), "doWebValidLogin");
            }
            return id;
        }
        #endregion

        #region web端买家注册
        public int doWebSendUser(string loginname, string pwd, string phone)
        {
            int id = 0;
            try
            {
                var ds = dal_buyer.GetList("loginname='" + loginname + "' and state = 1");
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return -1;
                }
                else
                {
                    id = dal_buyer.Add(loginname, pwd, phone);//Utils.Md5(pwd)
                }

            }
            catch (Exception e)
            {
                Common.LogHelper.WriteLog(e.ToString(), "doWebSendUser");
            }
            return id;
        }
        #endregion

        #region 通过商户id获取商家信息

        public string getSellerById(string sellerid)
        {
            string result = Enmus.RETURN.FAIL.ToString();
            try
            {
                int _sellerid = Common.TypeConverter.StrToInt(sellerid);
                DataSet ds = dal_seller.GetList("id=" + sellerid);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("Seller", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "getSellerById");
            }
            return result;
        }
        #endregion

        #region 修改商户

        public string modifySeller(string sellerid, string arrivtime, string floorprice, string phone, string tip, string average_spend)
        {
            string result = Enmus.RETURN.FAIL.ToString();
            try
            {
                int _sellerid = Common.TypeConverter.StrToInt(sellerid);
                Model.Seller modelSeller= dal_seller.GetModel(_sellerid);
                if (modelSeller != null)
                {
                    modelSeller.arrivtime = Common.TypeConverter.StrToInt(arrivtime);
                    modelSeller.floorprice = Convert.ToDecimal(floorprice);
                    modelSeller.phone = phone;
                    modelSeller.tip = Convert.ToDecimal(tip);
                    modelSeller.average_spend = Convert.ToDecimal(average_spend);
                    bool re= dal_seller.Update(modelSeller);
                    if (re == true)
                    {
                        result = Enmus.RETURN.SUC.ToString();
                    }
                    else
                    {
                        result = Enmus.RETURN.FAIL.ToString();
                    }
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "modifySeller");
            }
            return result;
        }

        #endregion

        #region 修改商户

        public string modifySellerState(string sellerid)
        {
            string result = Enmus.RETURN.FAIL.ToString();
            try
            {
                int _sellerid = Common.TypeConverter.StrToInt(sellerid);
                Model.Seller modelSeller = dal_seller.GetModel(_sellerid);
                if (modelSeller != null)
                {
                    int state =Convert.ToInt32(modelSeller.state);
                    if (state == 0) modelSeller.state = 1;
                    if (state == 1) modelSeller.state = 0;
                    bool re = dal_seller.Update(modelSeller);
                    if (re == true)
                    {
                        result = Enmus.RETURN.SUC.ToString();
                    }
                    else
                    {
                        result = Enmus.RETURN.FAIL.ToString();
                    }
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "modifySellerState");
            }
            return result;
        }

        #endregion

        #region 模糊搜索商户 user.seller.search
        public string doGetSellerList(string seller)
        {
            string result = Enmus.RETURN.NULL.ToString();
            try
            {
                DataSet ds = dal_seller.GetList("shopname like '%" + seller + "%'");
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    return Json.DataTableToJson("SellerList", ds.Tables[0]);
                }
            }
            catch (Exception e)
            {
                result = Enmus.RETURN.ERROR.ToString();
                Common.LogHelper.WriteLog(e.ToString(), "doGetSellerList");
            }
            return result;
        }
        #endregion

        #region 获取商户类型

        public string getShopType()
        {
            string xml =string.Empty;
            try
            {
                System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
                xmldoc.Load(Utils.GetMapPath("~/admin/res/js/shoptype.xml"));
                xml = xmldoc.InnerXml;
            }
            catch (Exception)
            {
                throw;
            }
            return xml;
        }

        #endregion
    }
}
